package boot.spring.util;

import boot.spring.vo.BaseImportVo;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 参数校验工具类
 * @Author: junqiang.lu
 * @Date: 2024/6/20
 */
public class ValidateUtil {

    private ValidateUtil() {
    }

    /**
     * 校验
     * @param t
     * @param <T>
     * @return
     */
    public static <T> List<String> valid(T t){
        Validator validatorFactory = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<T>> errors = validatorFactory.validate(t);
        return errors.stream().map(error -> error.getMessage()).collect(Collectors.toList());
    }

    /**
     * 校验 Excel 导入对象
     * @param t
     * @param <T>
     * @return
     */
    public static <T> List<String> validExcel(T t){
        Validator validatorFactory = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<T>> errors = validatorFactory.validate(t);
        return errors.stream().map(error -> String.format("第%s行错误:%s",
                ((BaseImportVo)t).getRowNo(),error.getMessage())).collect(Collectors.toList());
    }
    public static String getSDay(int day){
        // 获取当前日期时间
        Date currentDate = new Date();

        // 创建 Calendar 对象，并设置为当前日期时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);

        calendar.add(Calendar.DAY_OF_YEAR, day);

        // 得到增加七天后的日期时间
        Date futureDate = calendar.getTime();

        // 将日期时间转换为字符串
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String futureDateStr = format.format(futureDate);

        return futureDateStr;
    }
    public static void main(String[] args) throws IOException {

//        Map<String, Object> param = new HashMap<>();
//        param.put("group", "test_web");
//        param.put("action", "getEncryptSign");
//        param.put("l","[{\"quantity\":2,\"customerInfos\":[{\"touristType\":\"老人\",\"certNo\":\"362133196402200632\",\"realName\":\"许远瑞\",\"authType\":\"IC\"},{\"touristType\":\"老人\",\"certNo\":\"362133195906100630\",\"realName\":\"钟先佳\",\"authType\":\"IC\"}],\"occStartTime\":\"06:00\",\"occEndTime\":\"12:00\",\"isGiftTicket\":\"F\",\"goodsCode\":\"TYMP20220704005223\",\"occDate\":\"2024-09-12\",\"ticketId\":1036},{\"quantity\":26,\"customerInfos\":[{\"certNo\":\"362133197609300619\",\"realName\":\"许远斌\",\"authType\":\"IC\"},{\"certNo\":\"36213319771202064X\",\"realName\":\"郑国华\",\"authType\":\"IC\"},{\"certNo\":\"360732198412180616\",\"realName\":\"廖佛生\",\"authType\":\"IC\"},{\"certNo\":\"452723198701270842\",\"realName\":\"卢旭燕\",\"authType\":\"IC\"},{\"certNo\":\"362133199001250624\",\"realName\":\"洪海凤\",\"authType\":\"IC\"},{\"certNo\":\"360732199203210617\",\"realName\":\"周小春\",\"authType\":\"IC\"},{\"certNo\":\"36213319810903091X\",\"realName\":\"曾祥远\",\"authType\":\"IC\"},{\"certNo\":\"362133198308180929\",\"realName\":\"廖桂英\",\"authType\":\"IC\"},{\"certNo\":\"362133197505170610\",\"realName\":\"钟先法\",\"authType\":\"IC\"},{\"certNo\":\"362133197602180626\",\"realName\":\"温金连\",\"authType\":\"IC\"},{\"certNo\":\"362131197709141114\",\"realName\":\"曾活明\",\"authType\":\"IC\"},{\"certNo\":\"362131198105201141\",\"realName\":\"谭夏秀\",\"authType\":\"IC\"},{\"certNo\":\"362133196810260626\",\"realName\":\"李玉连\",\"authType\":\"IC\"},{\"certNo\":\"36213319750927111X\",\"realName\":\"刘功明\",\"authType\":\"IC\"},{\"certNo\":\"362133197503041129\",\"realName\":\"陈石秀\",\"authType\":\"IC\"},{\"certNo\":\"362133197008100611\",\"realName\":\"温金明\",\"authType\":\"IC\"},{\"certNo\":\"362133197102060628\",\"realName\":\"廖金连\",\"authType\":\"IC\"},{\"certNo\":\"350583198410031147\",\"realName\":\"洪美蓉\",\"authType\":\"IC\"},{\"certNo\":\"350524198306206525\",\"realName\":\"黄柳华\",\"authType\":\"IC\"},{\"certNo\":\"362133198102270603\",\"realName\":\"刘爱兰\",\"authType\":\"IC\"},{\"certNo\":\"36073219990215022X\",\"realName\":\"刘蓝蓝\",\"authType\":\"IC\"},{\"certNo\":\"360732200012020613\",\"realName\":\"温雪民\",\"authType\":\"IC\"},{\"certNo\":\"362133197410010657\",\"realName\":\"周平生\",\"authType\":\"IC\"},{\"certNo\":\"360732199202280613\",\"realName\":\"许天长\",\"authType\":\"IC\"},{\"certNo\":\"362133197103212312\",\"realName\":\"谢元发\",\"authType\":\"IC\"},{\"certNo\":\"362133197709100657\",\"realName\":\"刘富祥\",\"authType\":\"IC\"}],\"occStartTime\":\"06:00\",\"occEndTime\":\"12:00\",\"isGiftTicket\":\"F\",\"goodsCode\":\"TYMP20220704005220\",\"occDate\":\"2024-09-12\",\"ticketId\":1036},{\"quantity\":2,\"customerInfos\":[{\"certNo\":\"360732200806110610\",\"realName\":\"胡绍康\",\"authType\":\"IC\"},{\"certNo\":\"360732200806300617\",\"realName\":\"曾庆锦\",\"authType\":\"IC\"}],\"occStartTime\":\"06:00\",\"occEndTime\":\"12:00\",\"isGiftTicket\":\"F\",\"goodsCode\":\"TYMP20220709005358\",\"occDate\":\"2024-09-12\",\"ticketId\":1036}]");
//        param.put("m",1725507636948l);
//        param.put("bind_client",0.3111406944444348);
//        String result = HttpUtils.JsonPost("http://127.0.0.1:5612/business-demo/invoke", JSON.toJSONString(param));
//        JSONObject json = JSON.parseObject(result);
////        String result = HttpUtil.get("http://127.0.0.1:5612/business-demo/invoke?group=test_web&action=lotsmg&a=6c30577e8f6b6693d8f66134a76fdad4&bind_client=0.24041740881486162");
//        System.out.println(result);
        String str = "[{\"quantity\":2,\"customerInfos\":[{\"touristType\":\"老人\",\"certNo\":\"362133196402200632\",\"realName\":\"许远瑞\",\"authType\":\"IC\"},{\"touristType\":\"老人\",\"certNo\":\"362133195906100630\",\"realName\":\"钟先佳\",\"authType\":\"IC\"}],\"occStartTime\":\"06:00\",\"occEndTime\":\"12:00\",\"isGiftTicket\":\"F\",\"goodsCode\":\"TYMP20220704005223\",\"occDate\":\"2024-09-12\",\"ticketId\":1036},{\"quantity\":26,\"customerInfos\":[{\"certNo\":\"362133197609300619\",\"realName\":\"许远斌\",\"authType\":\"IC\"},{\"certNo\":\"36213319771202064X\",\"realName\":\"郑国华\",\"authType\":\"IC\"},{\"certNo\":\"360732198412180616\",\"realName\":\"廖佛生\",\"authType\":\"IC\"},{\"certNo\":\"452723198701270842\",\"realName\":\"卢旭燕\",\"authType\":\"IC\"},{\"certNo\":\"362133199001250624\",\"realName\":\"洪海凤\",\"authType\":\"IC\"},{\"certNo\":\"360732199203210617\",\"realName\":\"周小春\",\"authType\":\"IC\"},{\"certNo\":\"36213319810903091X\",\"realName\":\"曾祥远\",\"authType\":\"IC\"},{\"certNo\":\"362133198308180929\",\"realName\":\"廖桂英\",\"authType\":\"IC\"},{\"certNo\":\"362133197505170610\",\"realName\":\"钟先法\",\"authType\":\"IC\"},{\"certNo\":\"362133197602180626\",\"realName\":\"温金连\",\"authType\":\"IC\"},{\"certNo\":\"362131197709141114\",\"realName\":\"曾活明\",\"authType\":\"IC\"},{\"certNo\":\"362131198105201141\",\"realName\":\"谭夏秀\",\"authType\":\"IC\"},{\"certNo\":\"362133196810260626\",\"realName\":\"李玉连\",\"authType\":\"IC\"},{\"certNo\":\"36213319750927111X\",\"realName\":\"刘功明\",\"authType\":\"IC\"},{\"certNo\":\"362133197503041129\",\"realName\":\"陈石秀\",\"authType\":\"IC\"},{\"certNo\":\"362133197008100611\",\"realName\":\"温金明\",\"authType\":\"IC\"},{\"certNo\":\"362133197102060628\",\"realName\":\"廖金连\",\"authType\":\"IC\"},{\"certNo\":\"350583198410031147\",\"realName\":\"洪美蓉\",\"authType\":\"IC\"},{\"certNo\":\"350524198306206525\",\"realName\":\"黄柳华\",\"authType\":\"IC\"},{\"certNo\":\"362133198102270603\",\"realName\":\"刘爱兰\",\"authType\":\"IC\"},{\"certNo\":\"36073219990215022X\",\"realName\":\"刘蓝蓝\",\"authType\":\"IC\"},{\"certNo\":\"360732200012020613\",\"realName\":\"温雪民\",\"authType\":\"IC\"},{\"certNo\":\"362133197410010657\",\"realName\":\"周平生\",\"authType\":\"IC\"},{\"certNo\":\"360732199202280613\",\"realName\":\"许天长\",\"authType\":\"IC\"},{\"certNo\":\"362133197103212312\",\"realName\":\"谢元发\",\"authType\":\"IC\"},{\"certNo\":\"362133197709100657\",\"realName\":\"刘富祥\",\"authType\":\"IC\"}],\"occStartTime\":\"06:00\",\"occEndTime\":\"12:00\",\"isGiftTicket\":\"F\",\"goodsCode\":\"TYMP20220704005220\",\"occDate\":\"2024-09-12\",\"ticketId\":1036},{\"quantity\":2,\"customerInfos\":[{\"certNo\":\"360732200806110610\",\"realName\":\"胡绍康\",\"authType\":\"IC\"},{\"certNo\":\"360732200806300617\",\"realName\":\"曾庆锦\",\"authType\":\"IC\"}],\"occStartTime\":\"06:00\",\"occEndTime\":\"12:00\",\"isGiftTicket\":\"F\",\"goodsCode\":\"TYMP20220709005358\",\"occDate\":\"2024-09-12\",\"ticketId\":1036}]";

        String params = "group=test_web&action=getEncryptSign&l="+str+"&m="+1725507636948l+"&bind_client=0.3111406944444348";
        String encodedString = URLEncoder.encode(params, "UTF-8");
        System.out.println(encodedString);
        URL url = new URL("http://127.0.0.1:5612/business-demo/invoke");
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpConn.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
        writer.write(params);
        writer.flush();
        writer.close();
        httpConn.getOutputStream().close();
        InputStream responseStream = httpConn.getResponseCode() / 100 == 2
                ? httpConn.getInputStream()
                : httpConn.getErrorStream();
        Scanner s = new Scanner(responseStream).useDelimiter("\\A");
        String response = s.hasNext() ? s.next() : "";
        System.out.println(response);
        JSONObject json = JSON.parseObject(response);
    }

}

